import { Card, Form, Input, Button, Checkbox, message } from 'antd'
import logo from '@/assets/logo.png'
import styles from './index.module.scss'
import { useDispatch } from 'react-redux'
import { setLoginToken } from '@/store/actions/login'
import { useState } from 'react'
import { useHistory } from 'react-router'

const Login = () => {
  const dispatch = useDispatch()
  const history = useHistory()
  const [isLoading, setIsLoading] = useState(false)
  const onFinish = async (values) => {
    console.log('Success:', values)
    setIsLoading(true)
    try {
      await dispatch(setLoginToken(values))
      message.success('登录成功', 1, () => {
        // let pathName = '/home'
        // if(history.location.state.from) {
        //   pathName = history.location.state.from
        // }
        let pathName = '/home'
        if (history.location.state) {
          pathName = history.location.state.from
        }
        // 跳转主页
        history.replace(pathName)
      })
    } catch (err) {
      if (err.response && err.response.data) {
        message.error(err.response.data.message, 3)
      }
    } finally {
      setIsLoading(false)
    }
  }

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo)
  }
  return (
    <div className={styles.root}>
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 登录表单 */}
        <Form
          validateTrigger="onBlur"
          name="basic"
          initialValues={{
            agree: true,
            mobile: '13811111111',
            code: '246810'
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off">
          <Form.Item
            name="mobile"
            rules={[
              {
                pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
                message: '请输入合法的手机号'
              },
              { required: true, message: '手机号不能为空' }
            ]}>
            <Input />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              { len: 6, message: '请输入正确的验证码' },
              { required: true, message: '验证码不能为空' }
            ]}>
            <Input />
          </Form.Item>

          <Form.Item
            name="agree"
            valuePropName="checked"
            rules={[
              {
                validator (rule, value) {
                  if (value) {
                    return Promise.resolve()
                  } else {
                    return Promise.reject(new Error('请阅读并同意协议'))
                  }
                }
              }
            ]}>
            <Checkbox>我已阅读并同意[隐私条款]和[用户协议]</Checkbox>
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Button type="primary" htmlType="submit" loading={isLoading}>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Login
